import { $delete, $get, $post, $put, requestOneFile } from "@js/request"
import { compressLicenseAndMobile, decompressLicenseAndMobile } from "@js/tool"

// 更新或新增当事人数据
export async function partyRequest (p) {
  if (p.front instanceof File) {
    const res = await requestOneFile(p.front)
    p.fileid = res.id
  }
  if (p.back instanceof File) {
    const res = await requestOneFile(p.back)
    p.fileid1 = res.id
  }
  if (!p.front) {
    p.fileid = null
  }
  if (!p.back) {
    p.fileid1 = null
  }
  compressLicenseAndMobile(p)
  const id = p.id
  delete p.id
  delete p.front
  delete p.back
  delete p.subject_type_tag
  let res
  if (id === undefined) {
    res = await $post('/api/xd/party', p) // 新增数据
  } else {
    res = await $put(`/api/xd/party/${id}`, p) // 更新数据
  }
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

// 删除当事人
export async function delPartyRequest (id) {
  const res = await $delete(`/api/xd/party/${id}`)
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

// 获取当事人信息
export async function getPartyRequest (id) {
  const res = await $get(`/api/xd/party/${id}`)
  decompressLicenseAndMobile(res.data) 
  return res.data
}

